<?php

use plugins\wx\WxResponseException;

function get_json($url)
{
    $ch = curl_init();

    // 设置URL和相应的选项
    curl_setopt($ch, CURLOPT_URL, $url); //$url设置需要请求的url
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,FALSE);
    //设置请求头
    $headerArray = ["Content-type:application/json;charset='utf8'","Accept:application/json"];
    curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray);

    // 抓取URL并把它传递给浏览器
    $wxResult = json_decode(curl_exec($ch), true);

    if ( array_key_exists('errcode', $wxResult))
    {
        //抛出微信接口错误
        throw new WxResponseException($wxResult['errcode'], $wxResult['errmsg']);
    }

    // 关闭cURL资源，并且释放系统资源
    curl_close($ch);

    return $wxResult;
}